﻿using SHKF.B2C.Model.Dto.ReturnOrder;
using SHKF.B2C.Model.Entity.LM;
using SHKF.B2C.Model.Entity.Order;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace SHKF.B2C.BLL.LM
{
    public class RetrunOrderBLL : LMBLL
    {
        public RetrunOrderBLL(SqlSugarClient db ) :
          base(db)
        {

           
        }

        public void SaveAll(ReturnOrderContentEntity orderEntity, List<ReturnOrderDetailEntity> orderDetails)
        {
            try
            {
                this.db.BeginTran();

                bool bExist = false;

                //保存定单
                long SN = this.SaveOrder(orderEntity, out bExist);

                //保存明细
                if (orderDetails != null)
                {
                    foreach (ReturnOrderDetailEntity orderDetail in orderDetails)
                    {
                        orderDetail.FKSN = SN;
                        this.SaveDetail(orderDetail);
                    }
                }
 
                this.db.CommitTran();

            }
            catch (Exception ex)
            {
                this.db.RollbackTran();
                throw ex;
            }

        }


        public void SaveAll(ReturnOrderContentEntity orderEntity, List<ReturnOrderDetailEntity> orderDetails, List<ReturnOrderAccountEntity> accountList)
        {
            try
            {
                this.db.BeginTran();

                bool bExist = false;

                //保存定单
                long SN = this.SaveOrder(orderEntity, out bExist);

                //保存明细
                if (orderDetails != null)
                {
                    foreach (ReturnOrderDetailEntity orderDetail in orderDetails)
                    {
                        orderDetail.FKSN = SN;
                        this.SaveDetail(orderDetail);
                    }
                }

                //账目
                if(accountList != null)
                {
                    foreach(ReturnOrderAccountEntity returnOrderAccountEntity in accountList)
                    {
                        returnOrderAccountEntity.FKSN = SN;
                        returnOrderAccountEntity.pDate = System.DateTime.Now;
                        this.SaveAccount(returnOrderAccountEntity);
                    }
                }

                this.db.CommitTran();

            }
            catch (Exception ex)
            {
                this.db.RollbackTran();
                throw ex;
            }

        }


        private long SaveOrder(ReturnOrderContentEntity order, out bool bExist)
        {
            long SN = 0;
            bExist = false;
            try
            {
                ReturnOrderContentEntity orderOld = this.db.Queryable<ReturnOrderContentEntity>()
                    .Where(oo => oo.ShopSN == order.ShopSN 
                         && oo.app_poi_code == order.app_poi_code
                                  && oo.order_id == order.order_id)
                    .First();

                order.pDate = DateTime.Now;
                if (orderOld != null)
                {
                    SN = orderOld.SN;
                    order.SN = SN;
                    int iResult = this.db
                        .Updateable<ReturnOrderContentEntity>(order)
                        .ExecuteCommand();

                    bExist = true;
                }
                else
                { 
                   SN = this.db.Insertable<ReturnOrderContentEntity>(order)
                        .ExecuteReturnBigIdentity();
                    bExist = false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return SN;
        }

        private long SaveDetail(ReturnOrderDetailEntity orderDetail)
        {
            long SN = 0;
            ReturnOrderDetailEntity orderDetailEntity = db.Queryable<ReturnOrderDetailEntity>()
                .Where(oo => oo.FKSN == orderDetail.FKSN && oo.app_medicine_code == orderDetail.app_medicine_code)
                .First();
            if (orderDetailEntity != null)
            {
                orderDetail.SN = orderDetailEntity.SN;
                int iResult = db.Updateable<ReturnOrderDetailEntity>(orderDetail)
                   .ExecuteCommand();
            }
            else
            {
                SN = db.Insertable<ReturnOrderDetailEntity>(orderDetail)
                      .ExecuteReturnBigIdentity();
            }

            return SN;
        }

        private long SaveAccount(ReturnOrderAccountEntity data)
        {
            long SN = 0;
            ReturnOrderAccountEntity accountEntity = db.Queryable<ReturnOrderAccountEntity>()
                .Where(oo => oo.FKSN == data.FKSN 
                    && oo.OrderSN == data.OrderSN 
                    && oo.Refund_ID == data.Refund_ID
                    && oo.KeyWord == data.KeyWord)
                .First();
            if (accountEntity != null)
            {
                data.SN = accountEntity.SN;
                int iResult = db.Updateable<ReturnOrderAccountEntity>(data)
                   .ExecuteCommand();
            }
            else
            {
                SN = db.Insertable<ReturnOrderAccountEntity>(data)
                      .ExecuteReturnBigIdentity();
            }

            return SN;
        }

        public List<ReturnOrderContentDto> Query(long shopSN, int status, DateTime create_start, DateTime create_end)
        {

            var cmdText = base.db
                .Queryable<ReturnOrderContentEntity>()
                .Select(it => new ReturnOrderContentDto())
                .Where(ww => ww.ShopSN == shopSN)
                .Where(w => w.ctime >= create_start && w.ctime <= create_end);
            if (status != 0)
            {
                cmdText.Where(w => w.status == status);
            };

            List<ReturnOrderContentDto> list = cmdText.ToList();

            return list;

        }

        public List<ReturnOrderDetailEntity> QueryDetail(long SN)
        {
            var cmdText = db.Queryable<ReturnOrderDetailEntity>()
               .Where(w => w.FKSN == SN);

            return cmdText.ToList();
        }

    }
}
